package com.itheima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.constants.BusinessConstants;
import com.itheima.common.dto.PageRequestDto;
import com.itheima.common.exception.LeadNewsException;
import com.itheima.common.util.RequestContextUtil;
import com.itheima.common.vo.PageResultVo;
import com.itheima.wemedia.dto.WmMaterialPageReqDto;
import com.itheima.wemedia.mapper.WmNewsMaterialMapper;
import com.itheima.wemedia.pojo.WmMaterial;
import com.itheima.wemedia.mapper.WmMaterialMapper;
import com.itheima.wemedia.pojo.WmNewsMaterial;
import com.itheima.wemedia.service.WmMaterialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.invoke.LambdaConversionException;
import java.util.List;

/**
 * @description <p>自媒体图文素材信息 业务实现</p>
 *
 * @version 1.0
 * @package com.itheima.wemedia.service.impl
 */
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private WmMaterialMapper wmMaterialMapper;
    @Autowired
    private WmNewsMaterialMapper wmNewsMaterialMapper;
    @Override
    public PageResultVo<WmMaterial> findPage(WmMaterialPageReqDto dto) {
        //参数校验
        IPage<WmMaterial> page = new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<WmMaterial> lqw = Wrappers.<WmMaterial>lambdaQuery();
        //2.1 素材必须是当前用户上传的，即user_id=登录用户id
        lqw.eq(WmMaterial::getUserId, RequestContextUtil.getUserId());
        //因为全部和收藏分页路径是一致的，所以需要判断是否被收藏
        if (dto.getIsCollection()==1){
            lqw.eq(WmMaterial::getIsCollection,dto.getIsCollection());
        }
        page = page(page,lqw);

        return PageResultVo.pageResult(page.getCurrent(),page.getSize(),page.getTotal(),page.getRecords());
    }

    /**
     * 取消收藏
     * @param id
     * @return
     */
    @Override
    public int cancelCollect(Integer id) {
        //将状态改为0
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setId(id);
        wmMaterial.setIsCollection(BusinessConstants.WmNewsConstants.ISNOT_COLLECTION);
        return wmMaterialMapper.updateById(wmMaterial);
    }

    /**
     * 收藏
     * @param id
     * @return
     */
    @Override
    public int collect(Integer id) {
        //将状态改为1
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setId(id);
        wmMaterial.setIsCollection(BusinessConstants.WmNewsConstants.IS_COLLECTION);
        return wmMaterialMapper.updateById(wmMaterial);
    }

    /**
     *删除素材
     * @param id  素材id
     * @return
     */
    @Override
    public int delete(Integer id) {
        //删除时需要判断一下是否和其他表有关联，有关联则不能删
        //根据素材表id查询关联表wm_news_material
        List<WmNewsMaterial>  list = wmNewsMaterialMapper.selectList(Wrappers.<WmNewsMaterial>lambdaQuery().eq(WmNewsMaterial::getMaterialId,id));
        System.out.println("====================="+list==null);
        if (list.size()>0){
//        if (CollectionUtils.isNotEmpty(list)){
            //说明有关联,不能删除
            throw new LeadNewsException("与文章有关联，无法删除");
        }
        return wmMaterialMapper.deleteById(id);
    }


}
